//
//  MCVipPageVc.swift
//  MCBodyBuildingPro
//
//  Created by Renlvshijie on 2023/5/4.
//

import UIKit
import SnapKit
import Kingfisher
import ObjectMapper
import Toast_Swift
import QMUIKit






class MCVipPageVc: MCBaseVc {
    
    let vipPageIdentifier:String = "vipPageIdentifier"
    
    @IBOutlet weak var vipNumCollectionView: UICollectionView!
    
    var mineGridView:QMUIGridView?
    
    var userProfile:MCUserProfileModel!
    
    @IBOutlet weak var proVipLabel: MCPaddingLabel!
    @IBOutlet weak var subTitleLabel: UILabel!
    @IBOutlet weak var usernameLabel: UILabel!
    @IBOutlet weak var headerImageView: UIImageView!
    
    var selectOne:Int = 0
    
    private var moneyDateSource:Array<MCViperCardModel> = [MCViperCardModel]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.qmui_color(withHexString: "#F7F7F9")
        self.navigationItem.title = "会员中心"
        //loadSubViews()
        
        //连续包年
        let RMBConYear = MCViperCardModel(JSON: [:])
        RMBConYear?.title = "连续包年"
        RMBConYear?.price = "98"
        RMBConYear?.subTitle = "约8元/每月"
        RMBConYear?.color = 1
        
        self.moneyDateSource.append(RMBConYear!)
        
        //12个月
        let RMBConFullMonth = MCViperCardModel(JSON: [:])
        RMBConFullMonth?.title = "12个月会员"
        RMBConFullMonth?.price = "128"
        RMBConFullMonth?.subTitle = "约10元/每月"
        RMBConFullMonth?.color = 0
        self.moneyDateSource.append(RMBConFullMonth!)
        
        //连续包季
        let RMBConSeason = MCViperCardModel(JSON: [:])
        RMBConSeason?.title = "连续包季"
        RMBConSeason?.price = "45"
        RMBConSeason?.subTitle = "约15元/月"
        RMBConSeason?.color = 0
        self.moneyDateSource.append(RMBConSeason!)
        
        
        //连续包月
        let RMBConMonth = MCViperCardModel(JSON: [:])
        RMBConMonth?.title = "连续包月"
        RMBConMonth?.price = "9"
        RMBConMonth?.subTitle = "5折优惠"
        RMBConMonth?.color = 0
        self.moneyDateSource.append(RMBConMonth!)
        
        
        let pageingLayout = UICollectionViewFlowLayout.init()
        pageingLayout.scrollDirection = .horizontal
        vipNumCollectionView.setCollectionViewLayout(pageingLayout, animated: true)
        
        vipNumCollectionView?.delegate = self
        vipNumCollectionView?.dataSource = self
        vipNumCollectionView?.showsHorizontalScrollIndicator = false
        vipNumCollectionView?.backgroundColor = UIColor.qmui_color(withHexString: "#F7F7F9")
        vipNumCollectionView?.register(UINib(nibName: "MCVipNumCollectionCell", bundle: Bundle.main), forCellWithReuseIdentifier: vipPageIdentifier)
        
        
        self.headerImageView.kf.setImage(with:URL(string:userProfile.avatar ?? ""))
        self.usernameLabel.text = userProfile.name
        let subTitleLabel = QMUILabel.init()
        var strPhone = userProfile.phone ?? ""
        if(strPhone.count == 11){
            let sub2 = strPhone.dropFirst(3).dropLast(4)
            strPhone = strPhone.replacingOccurrences(of: sub2,with: "****")
        }
        self.subTitleLabel.text = strPhone
        if userProfile?.memberExpireTime?.isEmpty == true {
            self.proVipLabel.isHidden = true
        }else{
            self.proVipLabel.isHidden = false
             
        }
        
        
        self.vipNumCollectionView.selectItem(at: IndexPath(row: 0, section: 0), animated: true, scrollPosition: .top)
        
        
        IAPHelper.shared.initPayments { b in
            print("+++++\(b)+++++++++")
        }
        
    }
    
    
    

    @IBAction func enterVipBtn(_ sender: UIButton) {
        self.view.makeToastActivity(.center)
        self .getRequestLoadWithData(orider: (self.selectOne + 1))
        
    }
    
    
    
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        let padding = UIEdgeInsets.init(top: self.qmui_navigationBarMaxYInViewCoordinator, left: 16 + self.view.safeAreaInsets.left, bottom: 24 + self.view.safeAreaInsets.bottom, right: 16 + self.view.safeAreaInsets.right)
        let contentWidth = CGRectGetWidth(self.view.bounds) - UIEdgeInsetsGetHorizontalValue(padding)
        self.mineGridView?.frame = CGRect(x: padding.left, y: padding.top + 410, width:contentWidth, height: QMUIViewSelfSizingHeight)
    }
    
    //请求充值
    func getRequestLoadWithData(orider:Int) -> Void {
        NetWorkRequest(MineVM.sendOrderNumber(orderType: orider)) { ResponseModel in
            
            let orderInfoList = MCVIPOrderInfoList.init(JSONString: ResponseModel.dataString)
            
            if orderInfoList?.id != nil {
                switch orider {
                case 1:
                    IAPHelper.shared.pay(pID: .payVip208, order: "\(String(describing: (orderInfoList?.id)!) )")
                case 2:
                    IAPHelper.shared.pay(pID: .payVip60, order: "\(String(describing: (orderInfoList?.id)!) )")
                case 3:
                    IAPHelper.shared.pay(pID: .payVip25, order: "\(String(describing: (orderInfoList?.id)!) )")
                case 4:
                    IAPHelper.shared.pay(pID: .payVip12, order: "\(String(describing: (orderInfoList?.id)!))")
                default:
                    IAPHelper.shared.pay(pID: .payVip12, order: "")
                }
            }
            
          
           
            
            
        } failureCallback: { ResponseModel in
            self.view.makeToast("网络失败",position: .center)
        }
    }
    
    
}

extension MCVipPageVc:UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout {
    
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: 120, height: 120)
    }
    
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.moneyDateSource.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell:MCVipNumCollectionCell = collectionView.dequeueReusableCell(withReuseIdentifier: vipPageIdentifier, for: indexPath) as! MCVipNumCollectionCell
        
        let monthYear = self.moneyDateSource[indexPath.row]
        
        
        cell.priceTitleLabel.text = monthYear.price ?? ""
        cell.titleInfoLabel.text = monthYear.title ?? ""
        cell.subTitleLabel.text = monthYear.subTitle ?? ""
        cell.layer.cornerRadius = 8
        cell.layer.masksToBounds = true
        if monthYear.color == 1 {
            cell.backgroundColor = UIColor.qmui_color(withHexString: "#35DE94")
            cell.priceTitleLabel.textColor = UIColor.qmui_color(withHexString: "#10151C")
            cell.priceIconLabel.textColor = UIColor.qmui_color(withHexString: "#10151C")
        }else{
            cell.backgroundColor = UIColor.white
            cell.priceTitleLabel.textColor = UIColor.qmui_color(withHexString: "#35DE94")
            cell.priceIconLabel.textColor = UIColor.qmui_color(withHexString: "#35DE94")
        }
         
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
        let monthYear = self.moneyDateSource[indexPath.row]
        monthYear.color = 0
        let cell:MCVipNumCollectionCell = collectionView.cellForItem(at: indexPath) as! MCVipNumCollectionCell
        
        cell.backgroundColor = UIColor.white
        cell.priceTitleLabel.textColor = UIColor.qmui_color(withHexString: "#35DE94")
        cell.priceIconLabel.textColor = UIColor.qmui_color(withHexString: "#35DE94")
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let monthYear = self.moneyDateSource[indexPath.row]
        monthYear.color = 1
        let cell:MCVipNumCollectionCell = collectionView.cellForItem(at: indexPath) as! MCVipNumCollectionCell
        cell.backgroundColor = UIColor.qmui_color(withHexString: "#35DE94")
        cell.priceTitleLabel.textColor = UIColor.qmui_color(withHexString: "#10151C")
        cell.priceIconLabel.textColor = UIColor.qmui_color(withHexString: "#10151C")
        
        self.selectOne = indexPath.row
    }
    
    
}
